package org.jypj.dev.controller;

import com.google.gson.Gson;
import org.jypj.dev.dao.ILoginInfoDao;
import org.jypj.dev.dao.ISettingDao;
import org.jypj.dev.model.LoginInfo;
import org.jypj.dev.model.Setting;
import org.jypj.dev.util.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ChenYu on 2017-06-02.
 */
@Controller
public class BaseController {

    @Resource
    private ISettingDao settingDao;

    @Resource
    private ILoginInfoDao loginInfoDao;

    private static ThreadLocal<ServletRequest> currentRequest = new ThreadLocal<ServletRequest>();
    private static ThreadLocal<ServletResponse> currentResponse = new ThreadLocal<ServletResponse>();

    @ModelAttribute
    protected void initReqAndRep(HttpServletRequest request, HttpServletResponse response) {
        currentRequest.set(request);
        currentResponse.set(response);
    }

    protected HttpServletRequest getRequest() {
        return (HttpServletRequest) currentRequest.get();
    }

    protected HttpServletResponse getResponse() {
        return (HttpServletResponse) currentResponse.get();
    }


    public Setting getSetting() {
        return settingDao.getSetting();
    }

    @RequestMapping("toImageShow.action")
    public String toImageShow(String imagePath, HttpServletRequest request) {
        request.setAttribute("imagePath", imagePath);
        return "back/commonJsp/imgShow";
    }

    /**
     * 查询登录信息
     *
     * @param loginId
     * @return
     */
    public LoginInfo getLoginInfo(String loginId) {

        return loginInfoDao.selectLoginInfoById(loginId);
    }

    /**
     * 初始化今天和明天的日期
     *
     * @param request
     */
    public void initTodayAndTom(HttpServletRequest request) {
        String todayString = DateUtils.getTodayString("yyyy-MM-dd");
        request.setAttribute("today", todayString);
        String tomorrowString = DateUtils.getTomorrowString("yyyy-MM-dd");
        request.setAttribute("tomorrow", tomorrowString);
    }
}
